package com.sz.web.account.service.serviceImp;

import com.sz.web.account.entity.AccountRole;
import com.sz.web.account.mapper.AccountRoleMapper;
import com.sz.web.account.service.AccountRoleService;
import com.sz.web.common.pojo.ReturnPojo;
import com.sz.web.util.ReturnUtil;
import com.sz.web.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AccountRoleServiceImp implements AccountRoleService {

    @Resource
    private AccountRoleMapper accountRoleMapper;

    /**
     * 角色列表查询
     *
     * @return
     */
    @Override
    public ReturnPojo roleQuery() {
        List<AccountRole> accountRoles = accountRoleMapper.selectAll();
        return ReturnUtil.success(accountRoles);
    }

    /**
     * 角色新增
     *
     * @return
     */
    @Override
    public ReturnPojo roleAdd(AccountRole accountRole) {
        if (StringUtils.isBlank(accountRole.getRoleName())) {
            return ReturnUtil.fail("角色名不能为空!");
        }
        AccountRole dbAccountRole = accountRoleMapper.selectByRoleName(accountRole.getRoleName());
        if (null != dbAccountRole) {
            return ReturnUtil.fail("角色名已存在!");
        }
        int addCount = accountRoleMapper.insert(accountRole);
        if (addCount == 1) {
            return ReturnUtil.success(accountRole);
        } else {
            return ReturnUtil.fail();
        }
    }

    /**
     * 角色删除-byId
     *
     * @param roleId
     * @return
     */
    @Override
    public ReturnPojo roleDel(Integer roleId) {
        if (null == roleId){
            return ReturnUtil.fail("未接收到合法数据!");
        }
        AccountRole accountRole = accountRoleMapper.selectByPrimaryKey(roleId);
        if (null == accountRole) {
            return ReturnUtil.fail("角色不存在!");
        }
        int delCount = accountRoleMapper.deleteByPrimaryKey(roleId);
        if (delCount > 0) {
            return ReturnUtil.success("删除成功!");
        }else {
            return ReturnUtil.fail("未删除数据!");
        }
    }
}
